package com.zbj.springboot.mybatis.dao.mapper;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.util.StringUtils;

/**
 * Mapper接口beanName生成策略<br/>
 * 生成规则：className(首字母小写)+postfix<br/>
 *
 * @author weigang
 * @create 2018-04-16
 */
public class MapperBeanNameGenerator implements BeanNameGenerator {

    /**
     * beanName后缀添加
     */
    private String postfix = "";

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        String beanClassName = definition.getBeanClassName();
        String beanName = beanClassName;
        int index = beanClassName.lastIndexOf(".");
        if (index != -1) {
            beanName = beanClassName.substring(index + 1);
        }

        //首字母小写
        beanName = StringUtils.uncapitalize(beanName);
        beanName += postfix;

        return beanName;
    }

    public String getPostfix() {
        return postfix;
    }

    public void setPostfix(String postfix) {
        this.postfix = postfix;
    }
}
